#DevBlog#

İmkanın sınırını zorlamadan imkansızı bilemezsin...

Belki bir gün, Başka bir evrende ya da ileride...
image

Unity Terrain Texturing

Merhabalar bugün yeni bir konu ile karşınızdayım. ==> Terrain Texturing

Bu konu hakkında Türkçe bir kaynağa rastlayamadım o yüzden bu işe de el atmaya karar verdim.

Hemen Başlayalım...

1) Tabiki de öncelikle bir proje oluşturarak başlamamız gerekiyor. Ben 'SimpleTerrainPainter'  adında oluşturdum.

2) Ardından kendime güzel bir Terrain(Arazi) oluşturdum. Şekilde görüldüğü gibi

3) Bundan sonra kendime 4 adet Terrain Layer oluşturdum. Bunlar 'sand,grass,rock ve snow'adında da anlaşılacağı üzere bunlar Terrainimizi boyayacağımız Texture'lar.

4) Artık kodlamaya geçelim.

5) TToolsScript adında yeni bir Script oluşturdum bu bir editor scripti olacak ve gerekli ayarlamaları yapacak.

6) İçeriği şöyle.

using UnityEngine;

public class TToolsScript : MonoBehaviour
{
    public Terrain terrain;
    public TerrainLayer sand, rock, grass, snow;
    public float grassHeight, grassSlope, snowHeight, snowHeightTwo;

    TerrainData t;

    public void Option()
    {
        t = terrain.terrainData;

        TerrainLayer[] myLayers = new TerrainLayer[4];
        myLayers[0] = sand;
        myLayers[1] = rock;
        myLayers[2] = grass;
        myLayers[3] = snow;

        t.terrainLayers = myLayers;

        grassHeight = grassHeight == 0.0f ? 0.01f : grassHeight;
        grassSlope = grassSlope == 0.0f ? 0.25f : grassSlope;
        snowHeight = snowHeight == 0.0f ? 0.65f : snowHeight;
        snowHeightTwo = snowHeightTwo == 0.0f ? 0.8f : snowHeightTwo;
    }

    public void TTexturing()
    {
        float[,,] splatMap = new float[t.alphamapWidth, t.alphamapHeight, t.alphamapLayers];

        for (int y = 0; y < t.alphamapHeight; y++)
        {
            for (int x = 0; x < t.alphamapWidth; x++)
            {
                float normY = (float)y / (t.alphamapHeight - 1);
                float normX = (float)x / (t.alphamapWidth - 1);

                float slope = t.GetSteepness(normY, normX) / 90.0f;
                float height = t.GetHeight(y, x) / t.size.y;

                splatMap[x, y, 0] = 1 - height;
                splatMap[x, y, 1] = Mathf.Clamp01(slope * 1.2f);

                if (height >= grassHeight && slope < grassSlope)
                {
                    splatMap[x, y, 2] = Mathf.Clamp01((1 - slope) / height);
                }

                if (height >= snowHeight)
                {
                    splatMap[x, y, 3] = slope / height;
                }
                else if (height >= snowHeightTwo)
                {
                    splatMap[x, y, 3] = height;
                }
            }
        }

        t.SetAlphamaps(0, 0, splatMap);
    }
}
  • Terrain değişkenine boyamak istediğimiz terrain'i atayacağız.
  • Terrain Layer için değişkenlerimi oluşturuyorum ve kod içerisinde atıyorum.
  • Varsayılan olarak slope(eğim) ve height(yükseklik) değerlerimi atıyorum.

7) Unity Terrain boyamak için 'alphamap' kullanılır  ne olğunu buradaki linkten öğrenebilirsiniz.

8) splatmap adındaki değişkenimle bu ayarları değişkenime atadım.

9) for döngüleri içinde ki bunlar terrain deki noktaları temsil eder. Birkaç hazır method ve kendimden birşeyler ile slope ve height değerlerini buldum.

10) Bunları SplatMap değişkenime atadım ve 'SetAlphamaps' ile bunu terrainime ayarladım.

Resimler altta

 

Ve son hali

 

==> Sonraki Gönderide görüşmek üzere.